package aom.ju.ss.view.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import aom.ju.ss.R;


public class TitleView extends ConstraintLayout {


    private AppCompatImageView r_img, r_img_two, back;
    private TextView l_title, title, r_title, r_title_two, r_title_three, back_name;
    private View line, root_view;

    public TextView getR_title_three() {
        return r_title_three;
    }

    public AppCompatImageView getR_img() {
        r_img.setVisibility(VISIBLE);
        return r_img;
    }

    public void setR_img(AppCompatImageView r_img) {
        this.r_img = r_img;
    }

    public AppCompatImageView getR_img_two() {
        r_img_two.setVisibility(VISIBLE);
        return r_img_two;
    }

    public void setR_img_two(AppCompatImageView r_img_two) {
        this.r_img_two = r_img_two;
    }

    public AppCompatImageView getBack() {
        back.setVisibility(VISIBLE);
        return back;
    }

    public void setBack(AppCompatImageView back) {
        this.back = back;
    }

    public TextView getL_title() {
        l_title.setVisibility(VISIBLE);
        return l_title;
    }

    public void setL_title(TextView l_title) {
        this.l_title = l_title;
    }

    public TextView getTitle() {
        title.setVisibility(VISIBLE);
        return title;
    }

    public void setTitle(TextView title) {
        this.title = title;
    }

    public TextView getR_title() {

        r_title.setVisibility(VISIBLE);
        return r_title;
    }

    public void setR_title(TextView r_title) {
        this.r_title = r_title;
    }

    public TextView getBack_name() {
        back_name.setVisibility(VISIBLE);
        return back_name;
    }

    public void setBack_name(TextView back_name) {
        this.back_name = back_name;
    }

    public View getLine() {
        line.setVisibility(VISIBLE);
        return line;
    }

    public void setLine(View line) {
        this.line = line;
    }

    public View getRoot_view() {
        root_view.setVisibility(VISIBLE);
        return root_view;
    }

    public void setRoot_view(View root_view) {
        this.root_view = root_view;
    }

    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttrs(context, attrs);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initAttrs(context, attrs);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.app_title, this, true);
        r_img = findViewById(R.id.r_img);
        r_img_two = findViewById(R.id.r_img_two);
        back = findViewById(R.id.back);

        back_name = findViewById(R.id.back_name);
        l_title = findViewById(R.id.l_title);
        title = findViewById(R.id.tv_title);
        r_title = findViewById(R.id.r_title);
        r_title_two = findViewById(R.id.r_title_two);
        r_title_three = findViewById(R.id.r_title_three);


        line = findViewById(R.id.line);
        root_view = findViewById(R.id.root_view);
        back.setOnClickListener(v -> {


            try {
                AppCompatActivity activity = (AppCompatActivity) getContext();
                activity.onBackPressed();
            } catch (Exception e) {
            }
        });

    }


    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        Drawable backIcon = typedArray.getDrawable(R.styleable.TitleView_l_img_icon);
        Drawable rightImgIcon = typedArray.getDrawable(R.styleable.TitleView_r_img_icon);
        Drawable rightImgTwoIcon = typedArray.getDrawable(R.styleable.TitleView_r_two_img_icon);


        String titleString = typedArray.getString(R.styleable.TitleView_title_txt);
        String liftTitleString = typedArray.getString(R.styleable.TitleView_l_title_txt);
        String rightTitleString = typedArray.getString(R.styleable.TitleView_r_title_txt);
        String rightTitleTwoString = typedArray.getString(R.styleable.TitleView_r_title_two_txt);
        String rightTitleThreeString = typedArray.getString(R.styleable.TitleView_r_title_three_txt);
        String backNameString = typedArray.getString(R.styleable.TitleView_back_name_txt);

        int titleStringColor = typedArray.getColor(R.styleable.TitleView_title_txt_color, ContextCompat.getColor(getContext(), R.color.c_323232));
        int liftTitleStringColor = typedArray.getColor(R.styleable.TitleView_l_title_txt_color, ContextCompat.getColor(getContext(), R.color.c_323232));
        int rightTitleStringColor = typedArray.getColor(R.styleable.TitleView_r_title_txt_color, ContextCompat.getColor(getContext(), R.color.c_323232));
        int backNameStringColor = typedArray.getColor(R.styleable.TitleView_back_name_txt_color, ContextCompat.getColor(getContext(), R.color.c_323232));
        int isLineColor = typedArray.getColor(R.styleable.TitleView_line_color, Color.parseColor("#e4e4e4"));
        int rootViewBackground = typedArray.getColor(R.styleable.TitleView_root_view_background, ContextCompat.getColor(getContext(), R.color.bg_white));
        int rightTitleTwoStringColor = typedArray.getColor(R.styleable.TitleView_r_title_txt_two_color, ContextCompat.getColor(getContext(), R.color.c_323232));
        int rightTitleThreeStringColor = typedArray.getColor(R.styleable.TitleView_r_title_txt_three_color, ContextCompat.getColor(getContext(), R.color.c_323232));

        try {
            root_view.setBackgroundColor(rootViewBackground);
        }catch (Exception e){

        }


        title.setTextColor(titleStringColor);
        l_title.setTextColor(liftTitleStringColor);
        r_title.setTextColor(rightTitleStringColor);

        r_title_three.setText(rightTitleThreeString);
        back_name.setTextColor(backNameStringColor);

        r_title_two.setTextColor(rightTitleTwoStringColor);
        r_title_two.setText(rightTitleTwoString);


        r_title_three.setText(rightTitleThreeString);
        r_title_three.setTextColor(rightTitleThreeStringColor);
        line.setBackgroundColor(isLineColor);

        boolean isLine = typedArray.getBoolean(R.styleable.TitleView_line, false);


        if (backIcon != null && back.getVisibility() == View.GONE) {
            back.setVisibility(VISIBLE);
            back.setImageDrawable(backIcon);


        }
        if (rightImgIcon != null && r_img.getVisibility() == View.GONE) {
            r_img.setVisibility(VISIBLE);
            r_img.setImageDrawable(rightImgIcon);
        }
        if (rightImgTwoIcon != null && r_img_two.getVisibility() == View.GONE) {
            r_img_two.setVisibility(VISIBLE);
            r_img_two.setImageDrawable(rightImgTwoIcon);
        }

        if (!TextUtils.isEmpty(titleString) && title.getVisibility() == View.GONE) {
            title.setVisibility(VISIBLE);
            title.setText(titleString);
        }
        if (!TextUtils.isEmpty(liftTitleString) && l_title.getVisibility() == View.GONE) {
            l_title.setVisibility(VISIBLE);
            l_title.setText(liftTitleString);
        }
        if (!TextUtils.isEmpty(rightTitleString) && r_title.getVisibility() == View.GONE) {
            r_title.setVisibility(VISIBLE);
            r_title.setText(rightTitleString);
        }

        if (!TextUtils.isEmpty(rightTitleTwoString) && r_title_two.getVisibility() == View.GONE) {
            r_title_two.setVisibility(VISIBLE);
            r_title_two.setText(rightTitleTwoString);
        }
        if (!TextUtils.isEmpty(rightTitleThreeString) && r_title_three.getVisibility() == View.GONE) {
            r_title_three.setVisibility(VISIBLE);
            r_title_three.setText(rightTitleThreeString);
        }

        if (!TextUtils.isEmpty(backNameString) && back_name.getVisibility() == View.GONE) {
            back_name.setVisibility(VISIBLE);
            back_name.setText(backNameString);
        }
        if (isLine && line.getVisibility() == View.GONE) {
            line.setVisibility(VISIBLE);
        }


        root_view.setPadding(0, typedArray.getBoolean(R.styleable.TitleView_ispadding, true) ? TopBar.getStatusBarHeight(context) : 0, 0, 0);
        typedArray.recycle();

    }


    public void setbackground(int rec) {
        root_view.setBackgroundColor(getResources().getColor(rec));
    }

    public void setTitle(int rec) {
        title.setVisibility(VISIBLE);
        title.setText(getResources().getString(rec));
    }

    public void setlTitle(int rec) {
        l_title.setVisibility(VISIBLE);
        l_title.setText(getResources().getString(rec));
    }

    public TextView getR_title_two() {

        r_title_two.setVisibility(VISIBLE);
        return r_title_two;
    }

    public void setRImgTwoIcon(int rec) {
        r_img_two.setVisibility(VISIBLE);
        r_img_two.setImageResource(rec);
    }

    public AppCompatImageView getRImgTwoView() {

        r_img_two.setVisibility(VISIBLE);
        return r_img_two;
    }

    public AppCompatImageView getBackImg() {
        back.setVisibility(VISIBLE);
        return back;
    }

    public TextView getBackName() {
        back_name.setVisibility(VISIBLE);
        return back_name;
    }

}
